package com.lin.fengxiu.util;

import com.lin.fengxiu.bo.PageCounter;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName CommonUtil
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/14 11:42
 * @Version 1.0
 */
public class CommonUtil {

    public static PageCounter convertToPageParameter(Integer start, Integer count){
        int pageNum = start / count;

        PageCounter pageCounter = PageCounter.builder()
                .page(pageNum)
                .count(count)
                .build();
        return pageCounter;
    }

    /**
     * @Description TODO 时间函数，用于生成 expired_time
     * @param calendar
     * @param seconds
     * @return
     */
    public static Calendar addSomeSeconds(Calendar calendar, int seconds){
        calendar.add(Calendar.SECOND,seconds);
        return calendar;
    }

    /**
     * @Description TODO 优惠券是否过期，优惠券从属于活动，活动过期则优惠券过期
     * @param date
     * @param start
     * @param end
     * @return
     */
    public static Boolean isInTimeLine(Date date, Date start, Date end){
        Long time = date.getTime();
        Long startTime = start.getTime();
        Long endTime = end.getTime();
        if( time > startTime && time < endTime){
            return true;
        }
        return false;
    }

    /**
     * @Description TODO 通过placedTime订单过期判断
     * @param createTime
     * @param period
     * @return
     */
    public static boolean isOutOfDate(Date createTime, Long period) {
        Long now = Calendar.getInstance().getTimeInMillis();
        Long startTimeStamp = createTime.getTime();
        Long periodMillSecond = period * 1000;
        if(now > (startTimeStamp + periodMillSecond)){
            return true;
        }
        return false;
    }

    /**
     * @Description TODO 通过expiredTime订单过期判断
     * @param expiredTime
     * @return
     */
    public static boolean isOutOfDate(Date expiredTime) {
        Long now = Calendar.getInstance().getTimeInMillis();
        Long expiredTimeStamp = expiredTime.getTime();
        if(now > expiredTimeStamp){
            return true;
        }
        return false;
    }

    /**
     * @Description TODO 生成时间戳
     * @return
     */
    public static String timestamp10(){
        Long timestamp13 = Calendar.getInstance().getTimeInMillis();
        String timestamp13Str = timestamp13.toString();
        return timestamp13Str.substring(0,timestamp13Str.length() - 3);
    }

    /**
     * @Description TODO 把数字的元转换为分
     * @param p
     * @return
     */
    public static String yuanToFenPlainString(BigDecimal p){
        p = p.multiply(new BigDecimal("100"));
        return CommonUtil.toPlain(p);
    }

    /**
     * @Description TODO 把数字的分转为字符串
     * @param p
     * @return
     */
    public static String toPlain(BigDecimal p){
        return p.stripTrailingZeros().toPlainString();
    }
}
